package com.lance.shiro.service;

import com.lance.shiro.mapper.GenericCrudMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

public abstract class GenericCrudService<T> {
    public Logger log = LogManager.getLogger(getClass());
    protected GenericCrudMapper mapper;

    public GenericCrudService(GenericCrudMapper mapper) {
        this.mapper = mapper;
    }

    public T fetchById(int id) {
        return (T)mapper.fetchById(id);
    }

    public List<T> fetchAll() {
        return mapper.fetchAll();
    }


    public T insert(T o) {
         if(mapper.insert(o) == 1)
            return o;
          else{
             log.error("insert count not equals 1");
             return null;
         }

    }

    public int update(T o) {
        return mapper.update(o);
    }

    public int delete(int id) {
        return mapper.delete(id);
    }

}